package org.panda.tcmoc.view;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.List;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.panda.tcmoc.persistence.dao.SpProductDao;
import org.panda.tcmoc.persistence.dao.impl.SpProductDaoImpl;
import org.panda.tcmoc.view.table.SpInquiryFilter;
import org.panda.tcmoc.view.table.SpInquiryTableLabelProvider;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class SpInquiryEditorPart extends EditorPart {

	public static final String ID = "org.panda.tcmoc.view.SpInquiryEditorPart"; //$NON-NLS-1$

	private SpProductDao _dao = new SpProductDaoImpl();

	private Text text;
	private Text text_1;
	private Table table;

	public SpInquiryEditorPart() {
	}

	/**
	 * Create contents of the editor part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		Label lblDealTicketNo = new Label(container, SWT.NONE);
		lblDealTicketNo.setBounds(23, 24, 97, 14);
		lblDealTicketNo.setText("Deal Ticket No");

		text = new Text(container, SWT.BORDER);
		text.setBounds(123, 21, 120, 19);

		Label lblCnyAmount = new Label(container, SWT.NONE);
		lblCnyAmount.setText("CNY Amount");
		lblCnyAmount.setBounds(268, 24, 97, 14);

		text_1 = new Text(container, SWT.BORDER);
		text_1.setBounds(371, 21, 179, 19);

		TableViewer tableViewer = new TableViewer(container, SWT.BORDER
				| SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {

				String dealTicketNo = table.getItem(table.getSelectionIndex())
						.getText(1);
				System.out.println("selected:" + dealTicketNo);
				OpenDetailEditorAction action = new OpenDetailEditorAction(
						getSite().getWorkbenchWindow());
				action.setDealTicketNo(dealTicketNo);
				action.run();
			}
		});
		table.setBounds(10, 130, 671, 367);
		table.setHeaderVisible(true);

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn_3.getColumn();
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("Begin Date");

		TableViewerColumn tableViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnDealTicketNo = tableViewerColumn.getColumn();
		tblclmnDealTicketNo.setWidth(100);
		tblclmnDealTicketNo.setText("Deal Ticket No");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnCnyAmount = tableViewerColumn_1.getColumn();
		tblclmnCnyAmount.setWidth(200);
		tblclmnCnyAmount.setText("CNY Amount");

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnCounterPartyName = tableViewerColumn_2.getColumn();
		tblclmnCounterPartyName.setWidth(100);
		tblclmnCounterPartyName.setText("Counter Party Name");

		tableViewer.setContentProvider(ArrayContentProvider.getInstance());
		tableViewer.setLabelProvider(new SpInquiryTableLabelProvider());
		tableViewer.setInput(_dao.getAllMasterRecords());

		MenuManager menuManager = new MenuManager();
		menuManager.add(new OpenDetailEditorAction(this.getSite()
				.getWorkbenchWindow()));
		Menu menu = menuManager.createContextMenu(table);
		table.setMenu(menu);

	}

	class OpenDetailEditorAction extends Action {
		private IWorkbenchWindow window;
		private String dealTicketNo;

		public OpenDetailEditorAction(IWorkbenchWindow window) {
			this.window = window;
			this.setText("Detail");
		}

		public String getDealTicketNo() {
			return dealTicketNo;
		}

		public void setDealTicketNo(String dealTicketNo) {
			this.dealTicketNo = dealTicketNo;
		}

		public void run() {
			try {
				SpDetailEditorInput input = new SpDetailEditorInput();
				input.setViewSp(_dao.getMasterByDealTicketNo(dealTicketNo));

				this.window.getActivePage().openEditor(input,
						SpDetailEditorPart.ID);

			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// Do the Save operation
	}

	@Override
	public void doSaveAs() {
		// Do the Save As operation
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		this.setSite(site);
		this.setInput(input);

	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
}
